#-*-coding:utf-8-*-
import requests ,json


from ITAdmin.settings import ZABBIX_HEADER, ZABBIX_SERVER, ZABBIX_PASSWORD, ZABBIX_USER


def login_zabbix():
    data = {
        "jsonrpc": "2.0",
        "method": "user.login",
        "params": {
            "user": ZABBIX_USER,
            "password": ZABBIX_PASSWORD
        },
        "id": 0,
    }
    res = requests.post(ZABBIX_SERVER, data=json.dumps(data), headers=ZABBIX_HEADER)
    token = json.loads(res.content)['result']
    return token


def get_zabbix_servers(token):
    data = {
        "jsonrpc": "2.0",
        "method": "host.get",
        "params": {
            "output": [
                "hostid",
                "host"
            ],
            "selectInterfaces": [
                "interfaceid",
                "ip"
            ]
        },
        "id": 2,
        "auth": token
    }
    res = requests.post(ZABBIX_SERVER, data=json.dumps(data), headers=ZABBIX_HEADER)
    servers = json.loads(res.content)['result']
    return servers


def get_zabbix_items(hostid, token):
    data = json.dumps({
        "jsonrpc": "2.0",
        "method": "item.get",
        "params": {
            "output": "extend",
            "hostids": hostid
        },
        "auth": token,
        "id": 1
    })
    result = requests.post(ZABBIX_SERVER, data=data, headers=ZABBIX_HEADER)
    items = json.loads(result.content)['result']
    return items